var util = require('../../utils/util.js')
const Monitor = require('../../utils/monitor.js').Monitor
Page(Monitor.hookApp({
  data: {
    imgUrl: getApp().globalData.imgUrl,
    choose: -1, //选择标签的下标
    addVal: '', //自定义标签内容
    hidden: true, //设置添加按钮的展示隐藏
    addFinish: false, //设置添加按钮的展示隐藏
    addName: '', //标签名字   -->  最终传的
    biaoqianList: ['家', '公司', '学校'],
    name: '',
    phone: '',
    position: '',
    chooseYes: null,
    address: '', //详细地址
    clicked: false,
    is_default: false
  },
  onShow() {
    let that = this
    console.log(that.data.position)
  
 
    wx.getSetting({
      success(res) {
      
        let statu = res.authSetting['scope.userLocation'];
        let openSetting='';
        if (statu == undefined || statu === true ){
            openSetting= 'getAdress'
        }else  if (statu==false) {
            openSetting='openSetting'
        } 
        that.setData({
          openSetting
        })
      }
    })
  },
  openSetting(e) {
    var that = this
    wx.openSetting({
      success(settingData) {

      }
    })
  },
  getAddress(e) { //获取详细地址
    this.setData({
      address: e.detail.value
    })
  },
  getVal(e) { //获取收货人和手机号码
    if (e.currentTarget.dataset.type == 'name') {
      this.setData({
        name: e.detail.value
      })
    } else {
      this.setData({
        phone: e.detail.value
      })
    }
  },
  reg(e) { //验证手机号
    let phone = this.data.phone
    if (!(/^1[34578]\d{9}$/.test(phone))) {
      this.setData({
        telIsRight: false
      })
      if (phone.length == 0) {
        wx.showToast({
          icon: 'none',
          title: '请输入手机号码',
        })
        return false;
      } else if (phone.length != 11) {
        wx.showToast({
          icon: 'none',
          title: '请输入正确的手机号码',
        })
        return false;
      }
    } else {
      this.setData({
        telIsRight: true
      })
    }
  },
  chooseBiaoQian(e) { //选择标签
    let choose = e.currentTarget.dataset.idx,
      tagVal = this.data.biaoqianList[choose]; //标签内容
    this.setData({
      clicked: true,
      choose,
      tagVal
    })
  },
  getAdress() { //点击所在区域跳出地图
    wx.chooseLocation({
      success: res => {
        this.setData({
          chooseYes: true,
          position: res.address,
          latitude: res.latitude,
          longitude: res.longitude
        })
      },
      fail:()=>{
        this.setData({
          openSetting: 'openSetting',
        })
      }
    })
  },
  addFlag() { //增加标签
    this.setData({
      hidden: !this.data.hidden
    })
  },
  getAddVal(e) { //添加自定义标签内容
    this.setData({
      addVal: e.detail.value
    })
  },
  sure(e) { //点击确定 添加标签
    let { addVal, hidden } = this.data
    if (!e.currentTarget.dataset.idx) {
      if (addVal.length <= 4 && addVal.length > 0) {
        this.setData({
          addName: addVal,
          hidden: true
        })
        wx.showToast({
          title: '添加成功'
        })
      } else if (addVal.length == 0) {
        this.setData({
          hidden: !hidden
        })
      } else {
        wx.showToast({
          title: '请填写4字标签',
          icon: 'none'
        })
      }
    } else {
      this.setData({
        hidden: !hidden
      })
    }
  },
  switch1Change(e) { //switch 改变事件
    this.setData({
      is_default: e.detail.value
    })
  },
  save() { //保存按钮
    let that = this
    wx.showModal({
      title: '提示',
      content: '是否确认保存',
      success(res) {
        if (res.confirm) {
          let {
            name,
            phone,
            address,
            position,
            latitude,
            longitude,
            is_default,
            tagVal,
            address_id,
            addName,
            clicked,
            tag
          } = that.data
          if (tag && tag != null && !clicked) {
            tagVal = tag
          }
          let data = {
            name, //  用户名
            phone, //  电话号
            address, //  详细地址
            position, //  所在区域
            latitude, //  经度纬度
            longitude, //  经度纬度
            is_default: is_default + '', //  默认
            address_id: address_id || '', //  编辑或者新建地址
            tag: tagVal || '', //  标签名
            flag: 'save',
            newtag: addName || '' //  new标签名
          }
          if (data.name && data.phone.length == 11 && data.address && data.position) {
            util.API('spwx/v1.Address/setAddress', data).then(({
              code
            }) => {
              if (code == 200) {
                wx.showToast({
                  title: '添加成功',
                  duration: 2000,
                })
                setTimeout(() => {
                  wx.navigateBack({
                    delta: 1
                  })
                }, 2000)
              } else {
                wx.showToast({
                  title: '添加失败'
                })
              }
            })
          } else {
            wx.showToast({
              icon: 'none',
              title: '请输入完整信息'
            })
          }
        }
      }
    })

  },
  onLoad(options) {
    console.log(options)
    if (options.adressInfo) { //点击笔进来的  
      let jieshouData = JSON.parse(options.adressInfo)
      this.setData(jieshouData)
      this.setData({
        is_default: jieshouData.is_default == 'true' ? true : false
      })
      if (this.data.newtag) {
        this.setData({
          addName: this.data.newtag,
          hidden: true
        })
      } else if (this.data.tag) {
        this.setData({
          choose: this.data.biaoqianList.findIndex((value) => {
            return value == this.data.tag;
          })
        })

      }
    }
  }
}))